<?php
require_lang('patients');
require_model('base.class');
class patient extends base {
    private $phone_time = 120;
    private $phone_multiple = 5;
    private $table = 'Patients';
    private $primary = 'PatientId';
    function __construct() {
        parent::__construct($this->table,$this->primary);
    }
    function CreatePatient($params) {
        return $this->insert_item($params);
    }
    function ModifyPatient($params, $where) {
        return $this->update_item($params, $where);
    }
    function PatientLogin() {

    }
    function GetPatientById($patientId) {
        return $this->get_item(array('PatientId'=>$patientId));
    }
    function GetPatientByUserName($username) {
        return $this->get_item(array('UserName'=>$username));
    }



    function user_name_check(array & $request, array & $response) {
        $user_name = isset($request['user_name']) ? $request['user_name'] : '';
        $result = $this->get_item(array('user_name'=>$user_name));
        if($result) {
            $response['data'] = array('code'=>0, 'msg'=>lang('user_name_is_exist'));
        }else{
            $response['data'] = array('code'=>1, 'msg'=>lang('user_name_is_not_exists'));
        }
        return $response['data'];
    }
    function user_phone_check(array & $request , array & $response) {
        $phone = isset($request['phone']) ? $request['phone'] : '';
        $result = $this->get_item(array('user_id'=>$phone));
        if($result) {
            $response['data'] = array('code'=>0, 'msg'=>lang('phone_is_registered'));
        }else{
            $response['data'] = array('code'=>1, 'msg'=>lang('phone_is_not_exists'));
        }
        return $response['data'];
    }
    function create_user($params) {
        return $this->insert_item($params);
    }
    function modify_user($params, $where) {
        return $this->update_item($params, $where);
    }
    function user_login($params){
        return $this->get_item($params);
    }
    function get_user_by_id($id){
        return $this->get_item(array('id'=>$id));
    }
    function get_user_by_user_name($user_name) {
        return $this->get_item(array('user_name'=>$user_name));
    }

    //发送验证码
    function send_phone_check_code(array & $request, array & $response ) {
        if(isset($request['phone']) && $request['phone']){
            $phone = $request['phone'];
            $num = $this->get_item(array('user_id'=>$phone));
            if(count($num) == 0) {
                if($this->ctx->get_session('phone_p_'.$phone, true)) {
                    $prv_send_time = $this->ctx->get_session('phone_p_'.$phone.'_time', true);
                    if($prv_send_time- time() >= $this->phone_time * $this->phone_multiple) {
                        $domain = $this->ctx->shop_conf->get('shop_domain');
                        setcookie('phone_p_'.$phone.'_time', '', -86400, '/', $domain);
                        return $response['data'] = array('code'=>'-1');
                    }
                }
                /*
                $code = random(6, true);
                $this->ctx->set_session('phone_p_'.$phone, $code, true);
                $this->ctx->set_session('phone_p_'.$phone.'_time', time()+$this->phone_time*$this->phone_multiple, true);

                $msg = '51天使网提醒您：您注册的手机号码的验证码为：'.$code.'，请在注册页面填写验证码完成注册。【51天使网】';
                $responseXML = load_model('sms/sms')->sendSMS($phone,iconv('UTF-8','GB2312',$msg));
                $this->ctx->log_error('patients_phone:'.$phone. '--->返回信息：'.$responseXML.'--->msg:'.$msg);
                $responseXML  = 1 ;

                if((int)$responseXML >= 0) {
                    $code = 1;
                } else {
                    $code = 0 ;
                }
                */
                return $response['data'] = array('code'=>isset($code) ? $code : 0, 'time'=>$this->phone_time);
            } else {
                return $response['data'] = array('code'=>'2', 'msg'=>lang('phone_is_registered'));
            }
        } else {
            return $response['data'] = array('code'=>'-1');
        }
    }

    function add_patient(array & $request, array & $response) {
        $phone = isset($request['phone']) ? base64_decode($request['phone']) : '';
        $pwd = isset($request['password']) ? base64_decode($request['password']) : '';
        $cpwd = isset($request['repassword']) ? base64_decode($request['repassword']) : '';
        $referral = isset($request['referral']) ? base64_decode($request['referral']) : '';
        //手机验证码
        $check_code = isset($request['check_code']) ? base64_decode($request['check_code']) : '';
        if (empty($phone) || !preg_match("/^(((1[1-9]{1})|(1[1-9]{1})|(1[1-9]{1}))+\d{9})$/", $phone)) {
            return $response['data'] = array('msg' => lang('the_format_of_the_mobile_is_error'), 'code' => 4);
        }
        if($pwd !== $cpwd) {
            return $response['data'] = array('code'=>2, 'msg'=>lang('not_the_same_password'));
        }
        if($phone) {
            $status = $this->get_item(array('user_id'=>$phone));
            if($status) {
                return $response['data'] = array('code'=>3, 'msg'=>lang('phone_is_exist'));
            }
        }
        $insert = array('user_id'=>$phone, 'password'=>md5($pwd));
        $r = $this->insert_item($insert);
        //注册是否成功
        if (!$r) {
            return $response['data'] = array('msg' =>lang('failure'), 'code' => 5);
        }
        $this->ctx->set_session('patient_user_name', $phone, true);
        return $response['data'] = array('msg' => lang('success'), 'code' => 1);
    }
}